package cn.sdc.sfw.dao.handler;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import cn.sdc.sfw.dao.exception.DaoException;

/**
 * @author sdc
 *
 */
public final class DbHandler {

	private DbHandler() {
	}

	//Start Connection
	public static void closeConnection(Connection connection) {
		if (connection == null) {
			return;
		}
		try {
			connection.close();
		} catch (SQLException ex) {
			throw new DaoException(DaoException.CONNECTION_CLOSE_ERROR);
		}
	}
	//End Connection
	
	//Start Statement
	public static PreparedStatement prepareStatement(
		Connection connection,
		String sql) {

		try {
			return connection.prepareStatement(sql);
		} catch (SQLException ex) {
			throw new DaoException(DaoException.CONNECTION_STATEMENT_ERROR);
		}
	}
	
	public static CallableStatement prepareCall(
		Connection connection,
		String sql) {

		try {
			return connection.prepareCall(sql);
		} catch (SQLException ex) {
			throw new DaoException(DaoException.CONNECTION_CALL_ERROR);
		}
	}
	public static void setFetchSize(Statement statement, int fetchSize) {
		try {
			statement.setFetchSize(fetchSize);
		} catch (SQLException ex) {
			throw new DaoException(ex);
		}
	}
	
	public static void setMaxRows(Statement statement, int maxRows) {
		try {
			statement.setMaxRows(maxRows);
		} catch (SQLException ex) {
			throw new DaoException(ex);
		}
	}
	
	public static void commitConnection(Connection connection) {
		if (connection == null) {
			return;
		}
		try {
			connection.commit();
		} catch (SQLException ex) {
			throw new DaoException(DaoException.CONNECTION_COMMIT_ERROR);
		}
	}
	
	public static void rollbackConnection(Connection connection) {
		if (connection == null) {
			return;
		}
		try {
			connection.rollback();
		} catch (SQLException ex) {
			throw new DaoException(DaoException.CONNECTION_ROLLBACK_ERROR);
		}
	}
	
	public static void closeStatement(Statement statement) {
		if (statement == null) {
			return;
		}
		try {
			statement.close();
		} catch (SQLException ex) {
			throw new DaoException(ex);
		}
	}
	//End Statement
	
	//Start ResultSet executeSql
	public static DatabaseMetaData getMetaData(Connection connection) {
		try {
			return connection.getMetaData();
		} catch (SQLException ex) {
			throw new DaoException(DaoException.CONNECTION_GET_METADATA_ERROR);
		}
	}
	
	public static ResultSet executeQuery(PreparedStatement ps) {
		try {
			return ps.executeQuery();
		} catch (SQLException ex) {
			throw new DaoException(DaoException.EXCCUTE_QUERY_ERROR);
		}
	}

	public static int executeUpdate(PreparedStatement ps) {
		try {
			return ps.executeUpdate();
		} catch (SQLException ex) {
			ex.printStackTrace();
			throw new DaoException(DaoException.EXCCUTE_UPDATE_ERROR);
		}
	}
	public static void closeResultSet(ResultSet resultSet) {
		if (resultSet == null) {
			return;
		}
		try {
			resultSet.close();
		} catch (SQLException ex) {
			throw new DaoException(ex);
		}
	}
	//End ResultSet executeSql
	
}
